#include "main.h"
#include "cJSON.h"
#include <mysql/mysql.h>



const char* StrJson  = "{"                            \
    "\"name\": \"mculover666\","                \
    "\"age\": 22,"                              \
    "\"weight\": 55.5,"                         \
    "\"address\":"                              \
    "{"                                         \
        "\"country\": \"China\","               \
        "\"zip-code\": 111111"                  \
    "},"                                        \
    "\"skill\": [\"c\", \"Java\", \"Python\",\"10086\"]," \
    "\"student\": null"                                  \
"}";                                            


int main(int argc, char *argv[])
{

    cJSON* root = cJSON_Parse(StrJson);

    cout << cJSON_Print(root) << endl;

    cout << "parse json object" << endl;

    cJSON* name_js = cJSON_GetObjectItem(root,"name");
    cJSON* age_js  = cJSON_GetObjectItem(root,"age");
    cJSON* weight_js = cJSON_GetObjectItem(root,"weight");
    cJSON* addr_js = cJSON_GetObjectItem(root,"address");
    cJSON* skill_js = cJSON_GetObjectItem(root,"skill");
    cJSON* stu_js = cJSON_GetObjectItem(root,"student");


    cout << "name: "    << cJSON_GetStringValue(name_js) << endl;
    cout << "age: "     << cJSON_GetNumberValue(age_js)  << endl;
    cout << "weight: "  << cJSON_GetNumberValue(weight_js)  << endl;

    cJSON* country_js = cJSON_GetObjectItem(addr_js,"country");
    cJSON* zipcode_js = cJSON_GetObjectItem(addr_js,"zip-code");

    cout << "country: "    << cJSON_GetStringValue(country_js) << endl;
    cout << "zip-code: "     <<cJSON_GetNumberValue(zipcode_js)  << endl;

    int arrSize =  cJSON_GetArraySize(skill_js);

    for(int pos = 0; pos < arrSize; pos++)
    {
        cJSON* tmp_js = cJSON_GetArrayItem(skill_js,pos);
        if(cJSON_IsNumber(tmp_js))
            cout << "skill:" << cJSON_GetNumberValue(tmp_js) <<endl;
        else if(cJSON_IsString(tmp_js))
            cout << "skill:" << cJSON_GetStringValue(tmp_js) << endl;
    }

    cout << "is student :" << (cJSON_IsTrue(stu_js) ? "ture": "false") << endl;


    cout << "create json object" << endl;

    cJSON_Delete(root);


    cJSON* root_js = cJSON_CreateObject();

    cJSON* name  = cJSON_AddStringToObject(root_js,"name","mculover666");

    cJSON_SetValuestring(name,"WuDianZhou");

    cJSON* age = cJSON_AddNumberToObject(root_js,"age",22);
    cJSON* weight = cJSON_AddNumberToObject(root_js,"weight",55.5);


    cJSON* address = cJSON_CreateObject();
    cJSON* country = cJSON_AddStringToObject(address,"country","China");
    cJSON* zip_code  = cJSON_AddNumberToObject(address,"zip-code",11111);
    cJSON_bool flag =  cJSON_AddItemToObject(root_js,"address",address);


    if(flag == true)
        cout << " add object succeed" << endl;

    cJSON* skill_array = cJSON_CreateArray();
    cJSON_AddItemToArray(skill_array,cJSON_CreateString("c"));
    cJSON_AddItemToArray(skill_array,cJSON_CreateString("Java"));
    cJSON_AddItemToArray(skill_array,cJSON_CreateString("Python"));
    cJSON_AddItemToArray(skill_array,cJSON_CreateNull());
    cJSON_AddItemToArray(skill_array,cJSON_CreateNumber(10086));
    cJSON_AddItemToArray(skill_array,cJSON_CreateBool(true));
    cJSON_AddItemToArray(skill_array,cJSON_CreateBool(false));

    const float farr[] = {10.,2.0,30.0};
    cJSON_AddItemToArray(skill_array,cJSON_CreateFloatArray(farr,3));
    
    cJSON* key =  cJSON_CreateObject();
    cJSON_AddStringToObject(key,"key","value");
    cJSON_AddItemToObject(skill_array,"key",key);

    cJSON_AddItemToObject(root_js,"skill",skill_array);

    cJSON_AddBoolToObject(root_js,"student",false);


    cout << cJSON_Print(root_js) << endl;






    
    return 0;
}


